package com.example.activity;

import java.io.File;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.R;
import com.example.entity.URLEntity;
import com.example.module.api.APIRequest;
import com.example.module.api.HttpMethod;
import com.example.module.api.HttpMultipartTask;
import com.example.module.api.HttpTaskReponse;
import com.example.util.StringUtil;
import com.example.util.URLParser;

public class PostDealActivity extends Activity {
	protected static final String TAG = "PostDealActivity";
	private Button cancelButton, submitButton;
	private EditText textProvider;
	private EditText textTitle;
	private EditText textURL;
	private EditText textPrice;
	private EditText textDescription;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_post_deal);

		String title = getIntent().getStringExtra("Title");
		String currentUrl = getIntent().getStringExtra("CurrentUrl");
		final String originFile = getIntent().getStringExtra("OriginFile");
		final String thumbFile = getIntent().getStringExtra("ThumbFile");
		
		URLEntity urlEntity = URLParser.parse(currentUrl);
		
		
		textTitle = (EditText) findViewById(R.id.textTitle);
		if (!StringUtil.isEmpty(title))
			textTitle.setText(title);
		
		textProvider = (EditText) findViewById(R.id.textProvider);
		textProvider.setText(urlEntity.getDomainHost());
		
		textPrice = (EditText) findViewById(R.id.textPrice);
		
		textDescription = (EditText) findViewById(R.id.textDescription);
		
		textURL = (EditText) findViewById(R.id.textURL);
		textURL.setText(currentUrl);
		
		cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		submitButton = (Button) findViewById(R.id.submitButton);
		submitButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(validateFields()) {
					HttpMultipartTask task = new HttpMultipartTask(PostDealActivity.this);
					String url = "http://54.68.14.161/api/article";
					HashMap<String, String> params = new HashMap<String, String>();
					params.put("member_id", "540d1e62f717a5367c000003");
					params.put("title", textTitle.getText().toString());
					params.put("price", textPrice.getText().toString());
					params.put("provider", textProvider.getText().toString());
					params.put("content", textDescription.getText().toString());
					params.put("link", textURL.getText().toString());
					
					HashMap<String, File> files = new HashMap<String, File>();
					files.put("thumbnail", new File(thumbFile));
					files.put("picture", new File(originFile));
					
					APIRequest request = new APIRequest(url, HttpMethod.POST, params, files);
					
					
					try {
						task.execute(request);
						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
		
	}

	protected boolean validateFields() {
		return true;
	}
}
